package com.aurora.oa.mycenter.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.utils.SharedPreferencesUtil;
import com.aurora.oa.utils.StringUtils;
import com.aurora.oa.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/6 0006.
 */
public class GestureSecretSettingActivity extends BaseActivity {

    @Bind(R.id.tb_is_gesture_secret_open)
    RadioButton tbIsGestureSecretOpen;
    @Bind(R.id.iv_change_gesture)
    TextView ivChangeGesture;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "手势设置", true);
        setMyContentView(R.layout.activity_lock_secret_setting);
        ButterKnife.bind(this);
        String gesturePassword = SharedPreferencesUtil.getString(GestureSecretSettingActivity.this,SharedPreferencesUtil.GESTURE_PASSWORD);
        //还未设置手势密码
        if(StringUtils.isNullString(gesturePassword)){
            tbIsGestureSecretOpen.setChecked(false);
        }else{
            tbIsGestureSecretOpen.setChecked(SharedPreferencesUtil.getBoolean(this, SharedPreferencesUtil.IS_GESTURE_OPEN,false));
        }
        tbIsGestureSecretOpen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(b){
                    SharedPreferencesUtil.saveBoolean(GestureSecretSettingActivity.this,SharedPreferencesUtil.IS_GESTURE_OPEN,true);
                    String gesturePassword = SharedPreferencesUtil.getString(GestureSecretSettingActivity.this,SharedPreferencesUtil.GESTURE_PASSWORD);
                    //还未设置手势密码，进入设置界面
                    if(StringUtils.isNullString(gesturePassword)){
                        startActivityForResult(new Intent(GestureSecretSettingActivity.this,SetLockActivity.class),1);
                    }
                }else{
                    SharedPreferencesUtil.saveBoolean(GestureSecretSettingActivity.this,SharedPreferencesUtil.IS_GESTURE_OPEN,false);
                }
            }
        });
    }

    @OnClick(R.id.iv_change_gesture)
    public void onClick() {
        String gesturePassword = SharedPreferencesUtil.getString(GestureSecretSettingActivity.this,SharedPreferencesUtil.GESTURE_PASSWORD);
        if(StringUtils.isNullString(gesturePassword)){
            ToastUtil.show(this,"请先开启并设置手势密码");
        }else{
            startActivity(new Intent(GestureSecretSettingActivity.this,ResetLockActivity.class));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1){
            String gesturePassword = SharedPreferencesUtil.getString(GestureSecretSettingActivity.this,SharedPreferencesUtil.GESTURE_PASSWORD);
            if(StringUtils.isNullString(gesturePassword)){
                //未设置成成功
                tbIsGestureSecretOpen.setChecked(false);
            }
        }
    }
}
